<template>
  <div class="ldm-info">
    <!-- 顶部：返回 | 详情-->
    <div class="page-header">
      <a-button type="link" class="back" @click="goBack">
        <LeftOutlined class="back-icon" /> 返回
      </a-button>
      <span class="sep">|</span>
      <span class="title">详情</span>
    </div>

    <!-- 新父级 -->
    <div class="content-area">
      <!-- 正文 -->
      <div class="body" v-if="detail">
        <div class="content">
          <div class="row">
            <div class="label">名称：</div>
            <div class="value">{{ detail.link_name || "-" }}</div>
          </div>

          <div class="row">
            <div class="label">链接：</div>
            <div class="value">
              <a
                v-if="detail.url"
                :href="detail.url"
                target="_blank"
                rel="noopener noreferrer"
              >
                {{ detail.url }}
              </a>
              <span v-else>-</span>
            </div>
          </div>

          <div class="row">
            <div class="label">备注：</div>
            <div class="value">{{ detail.memo || "-" }}</div>
          </div>

          <div class="row">
            <div class="label">启用状态：</div>
            <div class="value">
              <a-radio-group
                :value="detail.status === 'published' ? 'enabled' : 'disabled'"
                :disabled="true"
              >
                <a-radio value="enabled">启用</a-radio>
                <a-radio value="disabled">停用</a-radio>
              </a-radio-group>
            </div>
          </div>

          <div class="row">
            <div class="label">创建时间：</div>
            <div class="value">
              {{ formatDateTime(detail.date_created) || "-" }}
            </div>
          </div>
        </div>
      </div>

      <div v-else class="empty">加载中…</div>

      <!-- 底部操作 -->
      <div class="ldm-fixed-footer">
        <a-button type="link" class="link btn primary" @click="goEdit">编辑</a-button>
        <span class="sep">|</span>
        <a-button type="link" class="link btn ghost" @click="goBack">返回</a-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { message } from "ant-design-vue";
import { LeftOutlined } from "@ant-design/icons-vue";
import {
  getLinkDeletionById,
  type LinkDeletionItem,
} from "@/api/modules/linkDeletion.api";

const router = useRouter();
const route = useRoute();
const TAB_KEY = "linkDeletionManagement";
const id = route.query.id ? String(route.query.id) : "";

const detail = ref<LinkDeletionItem | null>(null);

/** 格式化为 YYYY-MM-DD HH:mm:ss */
function formatDateTime(input?: string | Date | null): string {
  if (!input) return "";
  const raw = typeof input === "string" ? input.trim() : input;
  if (
    typeof raw === "string" &&
    /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(raw)
  ) {
    return raw;
  }
  const d = new Date(raw as any);
  if (isNaN(d.getTime())) return String(input);
  const pad = (n: number) => String(n).padStart(2, "0");
  return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())} ${pad(
    d.getHours()
  )}:${pad(d.getMinutes())}:${pad(d.getSeconds())}`;
}

async function init() {
  if (route.query.row) {
    try {
      const row = JSON.parse(decodeURIComponent(String(route.query.row)));
      detail.value = {
        id: row.id,
        link_name: row.link_name,
        url: row.url,
        memo: row.memo,
        status: row.status === "enabled" ? "published" : "draft",
        date_created: row.date_created,
      };
      return;
    } catch {}
  }
  try {
    const data = await getLinkDeletionById(id);
    detail.value = data;
  } catch (e: any) {
    message.error(e?.message || "加载失败");
  }
}
onMounted(init);

/** 统一返回 */
function backTarget() {
  const backTo = route.query.backTo;
  const from = route.query.from as string | undefined;
  if (backTo === "tab" && from) {
    return { path: from, query: { tab: TAB_KEY } };
  }
  return { path: "/linkDeletionManagement" };
}
function goBack() {
  router.replace(backTarget());
}
function goEdit() {
  if (!detail.value) return;
  router.push({
    path: "/linkDeletionManagementPage",
    query: {
      ...(route.query.backTo
        ? { backTo: "tab", from: route.query.from, tab: TAB_KEY }
        : {}),
      mode: "edit",
      id: String(detail.value.id),
      row: encodeURIComponent(
        JSON.stringify({
          id: detail.value.id,
          link_name: detail.value.link_name,
          url: detail.value.url,
          memo: detail.value.memo,
          status: detail.value.status === "published" ? "enabled" : "disabled",
        })
      ),
    },
  });
}
</script>

<style lang="less" scoped>
@pad-x: 24px;
@wrap-max: 640px;
@label-w: 96px; 
@safe-bottom: env(safe-area-inset-bottom);

.ldm-info {
  background: #fff;
  border: 1px solid #eaeaea;
  border-radius: 6px;
  min-height: 100vh;
  min-height: 100dvh;
  position: relative;

  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

/* ================= 顶部 ================= */
.page-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 24px @pad-x;
  border-bottom: 1px solid #f0f0f0;
  flex: 0 0 auto;

  .back.ant-btn {
    display: inline-flex;
    align-items: center;
    padding-left: 24px;
    height: 24px;
    line-height: 24px;
    font-size: 14px;
    color: #81889c;

    .back-icon {
      font-size: 16px;
      color: inherit;
    }
  }
  .sep {
    height: 24px;
    line-height: 24px;
    color: #d9d9d9;
    user-select: none;
  }
  .title {
    height: 24px;
    line-height: 24px;
    font-size: 16px;
    font-weight: 500;
    color: #000;
  }
}

.divider,
.section-divider { display: none; }

/* 新父级滚动容器 */
.content-area {
  flex: 1 1 auto;
  min-height: 0;              
  display: flex;
  flex-direction: column;
  overflow-y: auto;            
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding-bottom: calc(16px + @safe-bottom);

  /* 自定义滚动条 */
  &::-webkit-scrollbar { width: 10px; height: 10px; }
  &::-webkit-scrollbar-thumb {
    background: rgba(0,0,0,0.15);
    border-radius: 6px;
    border: 2px solid transparent;
    background-clip: padding-box;
  }
  &::-webkit-scrollbar-thumb:hover { background: rgba(0,0,0,0.25); }
  &::-webkit-scrollbar-track { background: transparent; }
  scrollbar-width: thin;
  scrollbar-color: rgba(0,0,0,0.25) transparent;
}

.body {
  padding: 12px 0 0;
  flex: 0 0 auto;          
}

.content {
  padding: 8px @pad-x 12px 140px;
  max-width: @wrap-max;
}

.row {
  display: flex;
  align-items: center;
  min-height: 44px;
  padding: 6px 0;
  border-bottom: 1px solid #efefef;

  &:last-child { border-bottom: 0; }

  .label {
    width: @label-w;
    color: #666;
    flex: 0 0 auto;
    text-align: left;
  }
  .value {
    flex: 1 1 auto;
    color: #333;
    word-break: break-all;
  }

  :deep(.ant-radio-wrapper) { margin-right: 18px; }
}

/* 空状态：同样在滚动容器内 */
.empty {
  padding: 40px;
  text-align: center;
  color: #999;
  flex: 0 0 auto;
}

/* 底部操作 */
.ldm-fixed-footer {
  position: static;
  margin-top: 16px;
  padding: 12px @pad-x calc(12px + @safe-bottom) 36px;
  background: #fff;
  border-top: 1px solid #eaeaea;
  box-sizing: border-box;

  display: flex;
  align-items: center;
  justify-content: center;      
  gap: 12px;
  flex-wrap: wrap;

  /* 统一按钮外观与尺寸 */
  .link.btn {
    font-size: 14px;
    height: 36px;
    // min-width: 112px;         
    padding: 0 20px;
    line-height: 36px;
    border-radius: 8px;
    font-weight: 600;
    transition: box-shadow .2s ease, filter .2s ease, background .2s ease, border-color .2s ease;
  }

  .primary {
    background: #1677ff !important;
    border: 1px solid #1677ff !important;
    color: #fff !important;
  }
  .primary:hover {
    filter: brightness(1.02);
    box-shadow: 0 2px 8px rgba(22,119,255,0.2);
  }

  .ghost {
    background: #fafafa !important;
    border: 1px solid #e8e8e8 !important;
    color: #666 !important;
  }
  .ghost:hover {
    background: #fff !important;
    color: #595959 !important;
    border-color: #d9d9d9 !important;
  }

  .sep {
    margin: 0 8px;
    color: #e5e7eb;
    user-select: none;
    line-height: 24px;
    height: 24px;
    display: inline-flex;
    align-items: center;
  }
}

/* 小屏优化 */
@media (max-width: 576px) {
  .page-header { padding: 16px 12px; }
  .content  { padding: 8px 12px 12px 12px; }
  .ldm-fixed-footer { padding-left: 12px; padding-right: 12px; }
}
</style>
